﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BCCommon;
using System.Text;
using System.Net;
using Microsoft.AspNetCore.Mvc;
using BCDto.Sim.Company.JobTitle;
using BCTMSRESTful.Configs;
using BCService.Utils;
using BCService.Sim.Company.JobTitle;

namespace BCTMSRESTful.Controllers.Management.Organization.JobTitle
{
    /// <summary>
    /// 职位
    /// </summary>
    [ApiExplorerSettings(GroupName = "Admin")]
    [Route("api/v1/sim/[controller]")]
    public class JobTitleController : ManagementBaseController
    {
        private readonly IJobTitleService jobTitleService;

        public JobTitleController(IJobTitleService jobTitleService)
        {
            this.jobTitleService = jobTitleService;
        }

        #region 添加职位
        /// <summary>
        ///  添加职位
        /// </summary>
        /// <param name="requestDto">职位信息</param>
        /// <response code="201">添加职位成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPost]
        [Log("添加职位")]
        [ProducesResponseType(typeof(JobTitleDto), 201)]
        public IActionResult Post(
            [FromBody] JobTitleRequestDto requestDto)
        {
            var result = this.jobTitleService.Add(requestDto,this.Admin);
            return StatusCode((int)HttpStatusCode.Created, result);
        }
        #endregion

        #region 修改职位
        /// <summary>
        ///  修改职位
        /// </summary>
        /// <param name="jobTitleId">职位Id</param>
        /// <param name="requestDto">职位信息</param>
        /// <response code="200">修改职位成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPut("{job_title_id}")]
        [Log("修改职位")]
        [ProducesResponseType(typeof(JobTitleDto), 200)]
        public IActionResult Put([FromRoute(Name = "job_title_id")] int jobTitleId, [FromBody] JobTitleRequestDto requestDto)
        {
            JobTitlePutDto putDto = requestDto.As<JobTitlePutDto>();
            putDto.JobTitleId = jobTitleId;
            return StatusCode((int)HttpStatusCode.OK, this.jobTitleService.Update(putDto,this.Admin));
        }
        #endregion

        #region 删除职位
        /// <summary>
        ///  删除职位
        /// </summary>
        /// <param name="jobTitleId">职位Id</param>
        /// <response code="204">删除职位成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpDelete("{job_title_id}")]
        [Log("删除职位")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult Delete([FromRoute(Name = "job_title_id")] int jobTitleId)
        {
            this.jobTitleService.Delete(jobTitleId);
            return StatusCode((int)HttpStatusCode.NoContent);
        }
        #endregion

        #region 获取职位
        /// <summary>
        ///  获取职位
        /// </summary>
        /// <param name="jobTitleId">职位Id</param>
        /// <response code="200">获取职位</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("{job_title_id}")]
        [ProducesResponseType(typeof(JobTitleDto), 200)]
        public IActionResult Get([FromRoute(Name = "job_title_id")] int jobTitleId)
        {
            return StatusCode((int)HttpStatusCode.OK, this.jobTitleService.Get(jobTitleId));
        }
        #endregion

        #region 获取职位列表
        /// <summary>
        ///  获取职位列表
        /// </summary>
        /// <response code="200">获取职位列表</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(JobTitleDto), 200)]
        public IActionResult Get()
        {
            return StatusCode((int)HttpStatusCode.OK, this.jobTitleService.GetList());
        }
        #endregion

        #region 获取职位（分页）
        /// <summary>
        ///  获取职位（分页）
        /// </summary>
        /// <param name="jobTitle">职位名称</param>
        /// <param name="enabled">是否启用</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="sortName">排序字段</param>
        /// <param name="ascending">升序</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">当前页大小</param>
        /// <response code="200">获取职位（分页）</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<JobTitleDto>), 200)]
        public IActionResult Query(
            [FromQuery(Name = "job_title")]string jobTitle,
            [FromQuery(Name = "enabled")] bool? enabled,
            [FromQuery(Name = "start")] DateTime? start,
            [FromQuery(Name = "end")] DateTime? end,
            [FromQuery(Name = "sort_name")] string sortName,
            [FromQuery(Name = "ascending")] bool? ascending,
            [FromQuery(Name = "page_index")] int pageIndex = 1,
            [FromQuery(Name = "page_size")] int pageSize = 10)
        {
            return StatusCode((int)HttpStatusCode.OK, this.jobTitleService.Query(jobTitle,enabled, start, end, sortName, ascending, pageIndex, pageSize));
        }
        #endregion

        #region 启用/禁用职位
        /// <summary>
        ///  启用/禁用职位
        /// </summary>
        /// <param name="jobTitleId">职位Id</param>
        /// <param name="enabled">是否启用</param>
        /// <response code="204">启用/禁用职位成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPut("[action]/{job_title_id}/{enabled}")]
        [Log("启用/禁用职位")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult Enabled([FromRoute(Name = "job_title_id")] int jobTitleId,
            [FromRoute(Name = "enabled")] bool enabled)
        {
            if (enabled)
            {
                this.jobTitleService.Enabled(jobTitleId);
                return StatusCode((int)HttpStatusCode.NoContent);
            }
            else
            {
                this.jobTitleService.Disabled(jobTitleId);
                return StatusCode((int)HttpStatusCode.NoContent);
            }
        }
        #endregion
    }
}
